package com.study.project.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

/**
 * @program: studyProject
 * @see:
 * @author: yangjie
 * @create: 2024-04-30 10:28
 **/
@Configuration
@EnableRetry
public class CommonConfig {
    @Bean
    public RetryTemplate installTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();

        // 重试策略,指定重试5次
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(5);
        retryTemplate.setRetryPolicy(retryPolicy);

        ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
        backOffPolicy.setInitialInterval(3000);
        backOffPolicy.setMultiplier(2);
        backOffPolicy.setMaxInterval(15000);

        retryTemplate.setBackOffPolicy(backOffPolicy);
        return retryTemplate;
    }
}